﻿using System.Data.SQLite;
using System.Windows.Input;
using Microsoft.Win32;

namespace Theodorus
{
	/// <summary>
	/// Interaction logic for ConnectionDialog.xaml
	/// </summary>
	public partial class ConnectionDialog 
	{
		private readonly SQLiteConnectionStringBuilder _builder = new SQLiteConnectionStringBuilder();
		
		public ConnectionDialog()
		{
			InitializeComponent();
			DataContext = _builder;
		}

		private void ExecuteOk(object sender, ExecutedRoutedEventArgs e)
		{
			DialogResult = true;
			Close();
		}

		private void CanExecuteOk(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = this.ValidateAllBindings();
		}

		private void ExecuteCancel(object sender, ExecutedRoutedEventArgs e)
		{
			DialogResult = false;
			Close();
		}

		private void ExecuteBrowse(object sender, ExecutedRoutedEventArgs e)
		{
			var ofd = new OpenFileDialog
			{
				AddExtension = false,
				CheckFileExists = false,
				CheckPathExists = true,
				DefaultExt = ".sqlite",
				Filter = "Database Files (*.sqlite)|*.sqlite|Database Files (*.db)|*.db|All Files (*.*)|*.*",
				FilterIndex = 0,
				RestoreDirectory = true,
				Multiselect = false,
				DereferenceLinks = true,
				ValidateNames = true
			};

			var result = ofd.ShowDialog();
			if (result == true)
			{
				Filename.Text = ofd.FileName;
			}
		}

		public string ConnectionString
		{
			get { return _builder.ConnectionString; }
			set { _builder.ConnectionString = value; }
		}

		
	}
}
